package com.example.android_demo_2;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

import android.app.Application;
import android.text.TextUtils;

public class VolleySingleton extends Application {

	public static final String TAG = VolleySingleton.class.getSimpleName();

	private RequestQueue requestQueue;
	private ImageLoader imageLoader;

	private static VolleySingleton instance;

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		NukeSSLCerts.nuke();
	}

	public static synchronized VolleySingleton getInstance() {
		return instance;
	}

	public RequestQueue getRequestQueue() {
		if (requestQueue == null) {
			requestQueue = Volley.newRequestQueue(getApplicationContext());
		}
		return requestQueue;
	}

	public ImageLoader getImageLoader() {
		getRequestQueue();
		if (imageLoader == null) {
			imageLoader = new ImageLoader(this.requestQueue, new LruBitmapCache());
		}
		return this.imageLoader;
	}

	public <T> void addToRequestQueue(Request<T> req, String tag) {
		req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
		getRequestQueue().add(req);
	}

	public <T> void addToRequestQueue(Request<T> req) {
		req.setTag(TAG);
		getRequestQueue().add(req);
	}

	public void cancelPendingRequests(Object tag) {
		if (requestQueue != null) {
			requestQueue.cancelAll(tag);
		}
	}

}
